
function checkConfigHost(dataId,a,id,fd,nbHostUp)
{
	var ipAddressToCheck,maskToCheck,ipAddressFromCheck,maskFromCheck,nodeIdTo,routerIdTo,nodeIdFrom,routerIdFrom,ipAddressHost;
	a.data.ospfstate = "down";
	var checkHost = "true";
	
	var returnArray = ["error","",0,""];
	
	var inputInterface = document.getElementById("interface"+dataId).value;
	var inputAddress = document.getElementById("addressip"+dataId).value;
	var inputMask = document.getElementById("mask"+dataId).value;
	
	inputInterface = escapeRegExp(inputInterface);
	inputAddress = escapeRegExp(inputAddress);
	inputMask = escapeRegExp(inputMask);
	
	ipAddressToCheck = inputAddress;
	maskToCheck = inputMask;
	
	a.data.nodeFromIpAddress = ipAddressToCheck;
	a.data.nodeFromMask = maskToCheck;
	
	a.data.nodeToIpAddress = ipAddressToCheck;
	a.data.nodeToMask = maskToCheck;
	
	nodeIdTo = a.data.nodeTo;		
	nodeIdFrom = a.data.nodeFrom;
	
	routerIdTo = fd.graph.nodes[nodeIdTo].data.ospfRouterId;
	routerIdFrom = fd.graph.nodes[nodeIdFrom].data.ospfRouterId; 
	
	var nodeToAdj = fd.graph.nodes[nodeIdTo].adjacencies;
	var len = 0;
	for(var ida in nodeToAdj) {
		len++;
	}
	if(len == 1)
	{
		nodeToAdj = fd.graph.nodes[nodeIdFrom].adjacencies;
	}
	
	for(var ida in nodeToAdj) {
		var a1 = nodeToAdj[ida]; 
		var adjtype = a1.data.type;
		// equalize ip address from and to
		a1.data.nodeToIpAddress = egalize(a1.data.nodeToIpAddress,a1.data.nodeFromIpAddress);
		a1.data.nodeFromIpAddress = a1.data.nodeToIpAddress;
		
		// equalize mask from and to
		a1.data.nodeToMask = egalize(a1.data.nodeToMask,a1.data.nodeFromMask);
		a1.data.nodeFromMask = a1.data.nodeToMask;
	

		if(adjtype == "switch-router") {  
			ipAddressFromCheck = a1.data.nodeToIpAddress;
			maskFromCheck = a1.data.nodeToMask;
		} else if(adjtype == "host-switch"){
			ipAddressHost = a1.data.nodeToIpAddress;
			if(ipAddressHost == ipAddressToCheck){
				if(checkHost == "false"){
					returnArray[1] = " Two or more hosts have the same Ip Interface : "  + ipAddressHost  + ".<br>";
					return returnArray;
				}
				else {checkHost = "false"}
			}
		}
	}
	
	// interfaces have the same Ip Address
	if(ipAddressToCheck == ipAddressFromCheck)
	{
		returnArray[1] = " Same Ip Interface : "  + ipAddressToCheck + " == " +ipAddressFromCheck+ " .<br>";		
		return returnArray;
	}
	// submasks do not match
	if(maskFromCheck != maskToCheck)
	{
		returnArray[1] = " Bad Mask : "  + maskToCheck + " != " + maskFromCheck+ " .<br>";		
		return returnArray;
	}	
	// Address or submask are not defined properly
	var networkAddressTo = getNetWorkAddress(ipAddressToCheck,maskToCheck);
	var networkAddressFrom = getNetWorkAddress(ipAddressFromCheck,maskFromCheck);
	if(networkAddressTo == "error" || networkAddressFrom == "error")
	{
		returnArray[1] = "Address or mask error : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";		
		return returnArray;
	}
	// One or two masks are not defined properly
	if (!checkMask(maskToCheck) || !checkMask(maskFromCheck))
	{
		returnArray[1] = "Mask error : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";
		return returnArray;
	}
	if(networkAddressTo == ipAddressToCheck || networkAddressFrom == ipAddressFromCheck)
	{
		returnArray[1] = " Interfaces can not have the same Ip than their Network.  Please check it out : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";
		return returnArray;
	}
	// networks do not match
	if(networkAddressTo != networkAddressFrom)
	{
		returnArray[1] = "Interface with ip address " + ipAddressToCheck + " Mask:"+ maskToCheck + " is supposed to have the same network that the connecting interface with ip address " + ipAddressFromCheck + " Mask:"+ maskFromCheck + ". Please make sure that interfaces and their netmasks are configured properly. <br>";
		return returnArray;
	}
	// one or two of the interface Ip have the same address than the broadcast address
	var broadcastAddress = getBroadcastAddress(networkAddressTo,maskToCheck);
	if(broadcastAddress == ipAddressToCheck || broadcastAddress == ipAddressFromCheck)
	{
		returnArray[1] = " Interfaces can not have the same Ip than their Broadcast.  Please check it out : Ip Interface " + ipAddressToCheck + " mask : " + maskToCheck + " and  Ip Interface " + ipAddressFromCheck + " mask : " + maskFromCheck + " .<br>";
		return returnArray;
	}

	var ackMessage = " Interface " + ipAddressToCheck +  " and interface" + ipAddressFromCheck + " are ready ! ";
	
	//if state is up ?
	var stateValue = document.getElementById("ospfstate"+dataId).innerHTML;
	
	if(stateValue == "up")
	{
		a.data.ospfstate = "up";
		ackMessage += " <strong>Interface is up.</strong> ";
		
		var ospfcostnetwork = calculCost(a.data.bandwidth);
		var maskprefix = getmaskprefix(maskFromCheck);
		
		// returnArray[3] = '<direct id="' + nbRouterUp + '" network="' + networkAddressTo + '" cost="' + ospfcostnetwork + '" nexthop="' + ipAddressFromCheck + '" nodeid="' + nodeIdTo + '" mask="' + maskprefix + '" depth="0" ></direct>';
		// routerIdTo = routerIdFrom // nodeIdFrom = nodeIdTo
		returnArray[2] = 1; // +1 host up
	}
	else
	{
		ackMessage += " <strong>Interface is down.</strong> ";
	}
	ackMessage += "<br>";
	
	returnArray[0] = 'acknow';
	returnArray[1] = ackMessage; 
	
	return returnArray;
}


function escapeRegExp(string) {
    return string.replace(/([*-+?^=!:${}()|\[\]\/\\])/g, '');
}

function replaceAll(string, find, replace) {
  return string.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

function egalize(first, second){
	if(first != null && first != "undefined")
	{
		return first;
	}
	else if(second != null && second != "undefined")
	{
		return second;
	}
	else {return "undefined";}
}